<template>
    <div>
        <h3>公告添加</h3>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td align="right">标题</td>
                    <td><input type="text" v-model="queryInfo.title"/></td>
                </tr>
                <tr>
                    <td align="right">内容</td>
                    <td><textarea cols="50" rows="5" v-model="queryInfo.content"></textarea></td>
                </tr>
                <tr>
                    <td align="right">公告类型</td>
                    <td>
                        <select v-model="queryInfo.noticeType">
                            <option value="">请选择</option>
                            <option :value="1">服务协议</option>
                            <option :value="2">招股说明书</option>
                            <option :value="3">收购报告书</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td align="right">图片</td>
                    <td><input type="file" @change="Img"/>
                    <img v-if="queryInfo.photo!=''" :src="queryInfo.photo" alt="" width="100" height="100"></td>
                </tr>
                <tr>
                    <td align="right">状态</td>
                    <td>
                        <input type="radio" name="state" :value="1" v-model="queryInfo.state"/>是
                        <input type="radio" name="state" :value="0" v-model="queryInfo.state"/>否
                    </td>
                </tr>
                <tr>
                    <td align="right"></td>
                    <td><input type="button" value="添加" class="btn btn-success" @click="ok"/></td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import {ref,reactive,onMounted} from 'vue'
import axios from 'axios';
import moment from 'moment';
import {useRouter,useRoute} from 'vue-router'
const router=useRouter();
const route=useRoute();
//上传图片
const Img=(e:any)=>{
    let obj=e.target.files[0];
    let f=new FormData;
    f.append("file",obj);
    axios({
        url:'/api/Imgs/Img',
        method:'post',
        data:f
    })
    .then((res)=>{
        console.log(res);
        if(res.data=="不是图片"){
            alert('上传的文件不是图片');
            return;
        }
        if(res.data=="大于2M"){
            alert('图片大于2M');
            return;
        }
        queryInfo.photo=res.data;
        return;
    })
    .catch((err)=>{
        console.log(err);
    })
}
//添加
let queryInfo=reactive({
  "title": "",
  "content": "",
  "noticeType": "",
  "photo": "",
  "state": 1,
});

const ok=()=>{
    if(queryInfo.title==""){
        alert('标题不能为空');
        return;
    }
    if(queryInfo.noticeType==""){
        alert('类型不能为空');
        return;
    }
    if(queryInfo.state==null){
        alert('状态不能为空');
        return;
    }
    if(queryInfo.content.length>50){
        alert('公告标题内容不能大于50个字');
        return;
    }
    axios({
        url:'/api/Notice/AddNotice',
        method:'post',
        data:queryInfo
    })
    .then((res)=>{
        console.log(res);
        if(res.data>0){
            alert('添加成功');
            router.push('/noticeList');
        }
        else if(res.data==-1){
            alert('标题重复');
            return;
        }
        else{
            alert('添加失败');
            return;
        }
    })
    .catch((err)=>{
        console.log(err);
    })
}
</script>

<style scoped>

</style>